package com.esb.utils;

import java.io.UnsupportedEncodingException;

public final class ByteUtils {
	
	public static byte[] short2bytes(short v){
		return short2bytes(v, 2);
	}
			
	public static byte[] short2bytes(short v, int len){
		byte[] b = new byte[len];
		short2bytes(v, b, 0, 2);
		return b;
	}
	
	public static byte[] int2bytes(int v){
		return int2bytes(v, 4);
	}
	
	public static byte[] int2bytes(int v, int len){
		byte[] b = new byte[len];
		int2bytes(v, b, 0, 4);
		return b;
	}
	
	
	public static void short2bytes(short v, byte[] b, int off){
		short2bytes(v, b, off, 2);
	}
	
	public static void short2bytes(short v, byte[] b, int off, int len)
	{
		for(int i=0;i<len;i++){
			b[off + i] = (byte) (v >>> (8 * (len - 1 - i)));
		}
	}
	
	public static void int2bytes(int v, byte[] b, int off){
		int2bytes(v, b, off, 4);
	}
	
	public static void int2bytes(int v, byte[] b, int off, int len)
	{
		for(int i=0;i<len;i++){
			b[off + i] = (byte) (v >>> (8 * (len - 1 - i)));
		}
	}
	
	public static void long2bytes(long v, byte[] b, int off)
	{
		long2bytes(v, b, off, 8);
	}
	
	public static void long2bytes(long v, byte[] b, int off, int len)
	{
		for(int i=0;i<len;i++){
			b[off + i] = (byte) (v >>> (8 * (len - 1 - i)));
		}
	}
	
	public static byte[] string2bytes(String v){
		return string2bytes(v, 1);
	}
	
	public static byte[] string2bytes(String v, int byteOflength){
		try {
			byte[] origin = v.getBytes(Constants.DEFAULT_CHARSET);
			byte[] dest = new byte[origin.length+byteOflength];
			int2bytes(origin.length, dest, 0, 1);
		    System.arraycopy(origin, 0, dest, byteOflength, origin.length);
		    return dest;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static short bytes2short(byte[] b, int off){
		return bytes2short(b, off, 2);
	}
	
	public static short bytes2short(byte[] b, int off, int len)
	{
		int v = 0;
		for(int i=0;i<len;i++){
			v |= ((b[off + i] & 0xFF) << (8 * (len - 1 - i)));
		}
		return (short)v;
	}
	
	public static int bytes2int(byte[] b, int off){
		return bytes2int(b, off, 4);	
	}
	
	public static int bytes2int(byte[] b, int off, int len)
	{
		int v = 0;
		for(int i=0;i<len;i++){
			v |= ((b[off + i] & 0xFF) << (8 * (len - 1 - i)));
		}
		return v;
	}
	
	public static long bytes2long(byte[] b, int off){
		return bytes2int(b, off, 8);	
	}
	
	public static long bytes2long(byte[] b, int off, int len)
	{
		long v = 0;
		for(int i=0;i<len;i++){
			v |= ((b[off + i] & 0xFF) << (8 * (len - 1 - i)));
		}
		return v;
	}
	
	public static String bytes2string(byte[] b){
		return bytes2string(b, 1);
	}
	
	public static String bytes2string(byte[] b, int byteOflength){
		int length = bytes2int(b, 0, byteOflength);
		byte[] dest = new byte[length];
	    System.arraycopy(b, byteOflength, dest, 0, dest.length);
	    try {
			return new String(dest, Constants.DEFAULT_CHARSET);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static byte[] merge(byte[] source, byte[] dest) {
		if (source == null || source.length == 0)
			return dest;
		if (dest == null || dest.length == 0)
			return source;
		byte[] r = new byte[source.length + dest.length];
		System.arraycopy(dest, 0, r, 0, dest.length);
		System.arraycopy(source, 0, r, dest.length, source.length);
		return r;
	}
	
	/**
	 * 去掉前byteOflength字节代表的长度
	 * @param b
	 * @param byteOflength
	 * @return
	 */
	public static byte[] slice(byte[] b, int byteOflength){
		int length = bytes2int(b, 0, byteOflength);
		
		byte[] dest = new byte[b.length - length - byteOflength];
		System.arraycopy(b, length + byteOflength, dest, 0, dest.length);
		
		return dest;
	}
}
